package com.czk.rdd.operator.transform

import org.apache.spark.rdd.RDD
import org.apache.spark.{HashPartitioner, SparkConf, SparkContext}

/**
 * @Author:ChenZhangKun
 * @Date: 2021/12/9 16:10
 */
object PartitionBy {
  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("RDD")
    val sc = new SparkContext(sparkConf)

    val rdd = sc.makeRDD(List(1, 2, 3, 4),2)

    val rdd1: RDD[(Int, Int)] = rdd.map((_,1))
    // 隐士转换
    // 将数据按照指定 Partitioner 重新进行分区。Spark 默认的分区器是 HashPartitioner
    val hashRDD = rdd1.partitionBy(new HashPartitioner(2))

    hashRDD.saveAsTextFile("out")
  }
}
